package com.pizza.database.mapping;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.pizza.database.DBConnection;
import com.pizza.model.Diametre;

/**
 * Class DiametreDB, mapping avec la bdd.
 *
 */
public class DiametreDB implements Serializable {

	private DBConnection dbConnexion;

	public DiametreDB(DBConnection _dbConnexion)
	{
		dbConnexion = _dbConnexion;
	}
	
	public Diametre getDiametreById(int _id)
	{
		Connection connexion = dbConnexion.getConnection();
		Diametre retour = null;
		StringBuilder query = new StringBuilder();
		query
				.append("SELECT * ");
		query.append(" FROM diametres");
		query.append(" WHERE id=?");
		try
		{
			PreparedStatement pStatement = connexion.prepareStatement(query.toString());
			pStatement.setInt(1, _id);
			ResultSet rs = pStatement.executeQuery();
			rs.first();

			String description = rs.getString("description");
			int nbpersonnes = rs.getInt("nbpersonnes");
			
			retour = new Diametre(_id, description, nbpersonnes);
			
			pStatement.close();
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		return retour;
	}
}
